

import { forwardRef, useImperativeHandle ,useState} from 'react';

//使用forwardRef, useImperativeHandle
const Page2 = forwardRef((props, ref)=>{
    const sendMessage = () => {
        console.log('sending message')
    }
    let [msg] = useState(()=>{
        return "hahahaha"
    })
    // 将子组件的方法 暴露给父组件
    useImperativeHandle(ref, () => ({
        sendMessage,
        msg
    }))

    return <>
        <div>Page2</div>
        <div >哈哈哈啊哈哈哈</div>
    </>

})




//自定义 props 和 useImperativeHandle


// const Page2 = (props)=>{

//     const sendMessage = () => {
//         console.log('sending message')
//     }
//     let [msg,getmsg] = useState(()=>{
//         return "hahahaha"
//     })

//     useImperativeHandle(props.oneref,() => ({
//         sendMessage,
//         msg
//     }))

//     return <>
//         <div>Page2</div>
//     </>
// }

export default Page2